package util.delicious;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;

import util.io.FileInput;
import util.nlp.Dictionary;
import util.parser.DeliciousURLInfoPost;
import util.parser.JsonParser;
import util.preprocessing.dmozExtractor;

public class EntryExtraction {

	/**
	 * @param args
	 */

	static public LinkedList<String> pairs = new LinkedList<String>();

	/**
	 * Receives a list representing a delicious entry and generate bigrams from
	 * the tags in the entry
	 * 
	 * (context,content)
	 * 
	 * @param list
	 */
	static void generatePairs(LinkedList<String> list) {

		for (int i = 0; i < list.size() - 1; i++) {

			String t[] = list.get(i).trim().split("\t");
			for (int j = i + 1; j < list.size(); j++) {

				String m[] = list.get(j).trim().split("\t");

				if (t.length >= 4 && m.length >= 4) {

					pairs.add(t[3]);
					pairs.add(m[3]);

					pairs.add(m[3]);
					pairs.add(t[3]);

					// System.out.println(t[3] + "\t" + m[3]);
				}

			}

			if (t.length > 3) {

			}
		}

	}

	public static void extractEntity(String path, String filter[]) {

		FileInput in = new FileInput(path);
		String line = in.readString().trim();

		LinkedList<String> tmp = new LinkedList<String>();

		String previous = "";
		String t[] = line.split("\t");
		previous = t[2];

		boolean kids = false;

		tmp.add(line);

		double count = 0;
		while (line != null) {

			count++;
			System.out.println(count);
			previous = t[2].trim();
			line = in.readString();
			if (line != null) {

				line = line.trim();
				t = line.split("\t");

				if (!previous.equals(t[2].trim())) {

					if (kids) {
						// output entry

						// generatePairs(tmp);

						/*
						 * for (int i = 0; i < tmp.size(); i++) {
						 * System.out.println(tmp.get(i)); }
						 */
					}

					kids = false;
					tmp = new LinkedList<String>();

				}

				for (int i = 0; i < filter.length; i++) {
					// System.out.println(line);
					if (t.length > 3 && t[3].trim().equals(filter[i])) {

						kids = true;

					}

				}

				tmp.add(line);

			}

		}

	}

	public static void addDeliciousObjects(
			Hashtable<String, DmozEntryDescription> dmoz) {

		Enumeration<String> keys = dmoz.keys();

		int count = 0;
		int total = 0;

		while (keys.hasMoreElements()) {

			String key = keys.nextElement();
			DmozEntryDescription dm = dmoz.get(key);

			total++;
			dm.delicious = JsonParser.parseDeliciousInfoFromURL(dm.url);

			if (dm.delicious != null) {

				if (dm.delicious.getFrequent_tags() != null
						&& dm.delicious.getFrequent_tags().size() > 0)
					count++;

			}

		}

		System.out.println("Delicious coverage: " + count / total);

	}

	public static void extractEntityByURL(String path,
			Hashtable<String, DmozEntryDescription> dmoz, boolean matchDomain,
			boolean include_delicious) {

		FileInput in = new FileInput(path);
		String line = in.readString().trim();

		Dictionary dict = null;

		if (matchDomain) {

			dict = new Dictionary(dmoz);

		}

		LinkedList<String> tmp = new LinkedList<String>();

		String previous = "";
		String t[] = line.split("\t");
		previous = t[2];

		boolean kids = false;

		tmp.add(line);

		double count = 0;
		while (line != null) {

			previous = t[2].trim();
			line = in.readString();
			if (line != null) {

				line = line.trim();
				t = line.split("\t");

				if (!previous.equals(t[2].trim())) {

					if (kids) {
						// output entry

						// generatePairs(tmp);

						for (int i = 0; i < tmp.size(); i++) {
							// System.out.println(tmp.get(i));
						}

					}

					kids = false;
					tmp = new LinkedList<String>();

				}

				String matched = t[2].trim();

				if (matchDomain) {

					LinkedList<String> list = dict.lookFor(matched);

					if (list != null && list.size() > 0) {
						matched = list.get(0);

					}

				}

				if (t.length > 2 && dmoz.containsKey(matched)) {

					DmozEntryDescription dmoz_entry = dmoz.get(matched);
					
					
					String category  = getNCategory(dmoz_entry.topic.replace("Top/Kids_and_Teens/", ""),1);
					kids = true;
					if (t.length > 3)
						System.out.println(matched
								+ "\t"
								+ dmoz_entry.description.replace(
										".</d:Description>", "")
								+ "\t"
								+ dmoz_entry.title.replace("</d:Title>", "")
								+ "\t"
								+ category + "\t"
								+ t[3]);

				}

				tmp.add(line);

			}

		}

		// delicious objects
		if (include_delicious) {
			// addDeliciousObjects(dmoz);

		}

	//	System.out.println(count);

	}

	public static void getCoverage(String path, int pos) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		int count = 0;
		int total = -0;
		while (line != null) {

			String t[] = line.split("\t");

			total++;
			if (t.length > pos) {

				DeliciousURLInfoPost delicious = JsonParser
						.parseDeliciousInfoFromURL(t[pos]);

				if (delicious != null) {

					if (delicious.getFrequent_tags() != null
							&& delicious.getFrequent_tags().size() > 0)
						count++;

				}

			}

			line = in.readString();
		}

		System.out.println("Coverage: " + count / total);

	}

	public void convertDeliciousSingleEntry(String path) {

		FileInput in = new FileInput(path);
		String line = in.readString().trim();

		LinkedList<String> tmp = new LinkedList<String>();

		String previous = "";
		String t[] = line.split("\t");
		previous = t[2];

		Long id = new Long(0);

		tmp.add(line);

		while (line != null) {

			previous = t[2].trim();
			line = in.readString();
			if (line != null) {

				line = line.trim();
				t = line.split("\t");

				if (!previous.equals(t[2].trim())) {

					// output entry

					String temp[] = tmp.get(0).split("\t");
					System.out.print(id.toHexString(id) + "\t" + temp[0] + "\t"
							+ temp[1] + "\t" + temp[2] + "\t" + temp[3] + "\t");

					for (int i = 1; i < tmp.size(); i++) {
						System.out.print("\t" + tmp.get(i).split("\t")[3]);
					}

					System.out.println("");
					tmp = new LinkedList<String>();
					id++;
				}

				tmp.add(line);

			}

		}

	}
	
	
	
	public static String getNCategory(String cat, int n) {

		String c = "";

		String t[] = cat.split("/");

		
		
		for(int i =0; i < n && i<t.length; i++){
			
			
			c+=t[i] + "/";
			
		}
		

		return c;

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String path = "/home/sergio/projects/data/tu_berlin/130.149.154.91/corpus/delicious/delicious.txt";

		path = "/home/sergio/projects/data/delicious/experiments/entries_exactDMOZ.txt";

		String dmoz = "/home/sergio/projects/data/dmoz/kt/kt-content.rdf.u8";

		String a[] = new String[1];
		a[0] = "kids";
		// extractEntity(path, a);

		extractEntityByURL(path, dmozExtractor.getURLandInfo(dmoz, 1), true,	true);
		
		//System.out.println(getNCategory("People_and_Society/Biography/Civil_Rights_Leaders/King,_Martin_Luther,_Jr",2));

		dmoz = "/home/sergio/projects/data/dmoz/kt/dmoz_kids.txt";

		// getCoverage(dmoz,1);

	}

}
